package com.hhu.wangzb.server.service.impl;

import com.hhu.wangzb.common.entity.UserEntity;
import com.hhu.wangzb.server.service.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhu.wangzb.common.utils.PageUtils;
import com.hhu.wangzb.common.utils.Query;

import com.hhu.wangzb.server.dao.PvDistrictDao;
import com.hhu.wangzb.common.entity.PvDistrictEntity;


@Service("pvDistrictService")
public class PvDistrictServiceImpl extends ServiceImpl<PvDistrictDao, PvDistrictEntity> implements PvDistrictService {

    @Autowired
    PvAreaService pvAreaService;

    @Autowired
    PvComponentService pvComponentService;

    @Autowired
    InverterService inverterService;

    @Autowired
    BoxChangeService boxChangeService;

    @Autowired
    LowVolLineService lowVolLineService;

    @Autowired
    CollectorLineService collectorLineService;

    @Autowired
    FieldGroundingService fieldGroundingService;

    @Autowired
    ComponentBracketService componentBracketService;

    @Autowired
    PlaceService placeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PvDistrictEntity> page = this.page(
                new Query<PvDistrictEntity>().getPage(params),
                new QueryWrapper<PvDistrictEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PvDistrictEntity queryByParamsMap(Map<String, Object> paramsMap) {

        QueryWrapper<PvDistrictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(paramsMap);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {

        // 查询
        List<PvDistrictEntity> pvDistrictEntities = baseMapper.selectBatchIds(idList);

        // 存储包含子节点的实体id集合
        List<Integer> fgIds = new ArrayList<>();
        List<Integer> lvlIds = new ArrayList<>();

        for (PvDistrictEntity entity : pvDistrictEntities) {
            // 存储包含子节点的实体id集合（用于层次删除）
            fgIds.add(entity.getFgId());
            lvlIds.add(entity.getLvlId());
            // 直接删除不包含子节点的实体
            if (entity.getPaId() != null) pvAreaService.removeById(entity.getPaId());
            if (entity.getPcId() != null) pvComponentService.removeById(entity.getPcId());
            if (entity.getInId() != null) inverterService.removeById(entity.getInId());
            if (entity.getBcId() != null) boxChangeService.removeById(entity.getBcId());
            if (entity.getClId() != null) collectorLineService.removeById(entity.getClId());
            if (entity.getCbId() != null) componentBracketService.removeById(entity.getCbId());
            if (entity.getPlId() != null) placeService.removeById(entity.getPlId());
        }

        // 层次删除包含子节点的实体
        if (fgIds.size() != 0) fieldGroundingService.removeByIds(fgIds);
        if (lvlIds.size() != 0) lowVolLineService.removeByIds(lvlIds);

        // 删除自身
        return super.removeByIds(idList);
    }
}